package com.hndzyu.yd.my;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.MyMsg;
import com.hndzyu.yd.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YJ on 2018/10/23.
 */
public class MyMsgAdapter extends RecyclerView.Adapter<MyMsgAdapter.ViewHolder> {
    private Context mContext;
    private List<MyMsg.MessageBean.DataBean> mData = new ArrayList<>();

    public MyMsgAdapter(Context context) {
        mContext = context;
    }

    public void updateData(int page, List<MyMsg.MessageBean.DataBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page == 1){
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_msg, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MyMsg.MessageBean.DataBean dataBean = mData.get(position);
        //0未读  1已读
        if (dataBean.getStatus() == 0){
            holder.ivStates.setImageResource(R.mipmap.lb_red);
        } else {
            holder.ivStates.setImageResource(R.mipmap.lb);
        }
        holder.tvContent.setText(dataBean.getContent());
        holder.tvTime.setText(Util.stampToDate(dataBean.getAddtime()+""));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivStates;
        TextView tvContent;
        TextView tvTime;
        public ViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            ivStates = itemView.findViewById(R.id.iv_states);
        }
    }
}
